package dao;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import dao.bean.User;
import dao.dao.UserDao;
import dao.dao.UserDaoJdbc;
import dao.exception.NotUniqueUserEmailException;
import dao.exception.NotUniqueUserLoginException;

import java.sql.SQLException;

import static java.lang.Math.random;

public class Test {

    public static void main(String[] args) throws SQLException, NotUniqueUserEmailException, NotUniqueUserLoginException {
        UserDao dao = new UserDaoJdbc();
        // select
        System.out.println(dao.selectAll());
        { // login doubled
            try {
                User newUser = new User(-1);
                User existed = dao.selectAll().get(0);
                newUser.setLogin(existed.getLogin());
                newUser.setEmail(random() + existed.getEmail());
                dao.insert(newUser);
            } catch (MySQLIntegrityConstraintViolationException e) {
                System.out.println("e.getErrorCode() = " + e.getErrorCode());
                System.out.println("e.getSQLState() = " + e.getSQLState());
            }
        }
        { // email doubled
//            try {
//                User newUser = new User(-1);
//                User existed = dao.selectAll().get(0);
//                newUser.setLogin(random() + existed.getLogin());
//                newUser.setEmail(existed.getEmail());
//                dao.insert(newUser);
//            } catch (MySQLIntegrityConstraintViolationException e) {
//                System.out.println("e.getErrorCode() = " + e.getErrorCode());
//                System.out.println("e.getSQLState() = " + e.getSQLState());
//            }
        }
        { // unique
            User newUser = new User(-1);
            User existed = dao.selectAll().get(0);
            newUser.setLogin(existed.getLogin());
            newUser.setEmail(existed.getEmail());
            dao.insert(newUser);
        }
    }
}
